﻿/********************************************************
 * 类名： CustomerBLL.cs
 * 功能： 客户管理业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-11-27	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.SetManage.BasicData;
using PSS.Entity.SetManage.BasicData;
using PSS.Utility;

namespace PSS.BLL.SetManage.BasicData
{
    public class CustomerBLL
    {
        private readonly CustomerDAL _dal = new CustomerDAL();

        public void AddCustomer(string data)
        {
            var model = new CustomerEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<CustomerContactsEntity>();
            foreach (Hashtable row in rows)
            {
                model.CustomerId = Guid.NewGuid();
                model.CustomerCode = row["CustomerCode"].ToString();
                model.CustomerName = row["CustomerName"].ToString();
                model.CCategoryId = new Guid(row["CCategoryId"].ToString());
                model.BalanceDate = Convert.ToDateTime(row["BalanceDate"]);
                model.InitialRec = Convert.ToDecimal(row["InitialRec"]);
                model.InitialDeposit = Convert.ToDecimal(row["InitialDeposit"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new CustomerContactsEntity();
                    cmodel.CContactId = Guid.NewGuid();
                    cmodel.CustomerId = model.CustomerId;
                    cmodel.Linkman = item["Linkman"] != null ? item["Linkman"].ToString() : "";
                    cmodel.MobilePhone = item["MobilePhone"] != null ? item["MobilePhone"].ToString() : "";
                    cmodel.SpecialPlane = item["SpecialPlane"] != null ? item["SpecialPlane"].ToString() : "";
                    cmodel.QqMsn = item["QqMsn"] != null ? item["QqMsn"].ToString() : "";
                    cmodel.DeliveryAddress = item["DeliveryAddress"] != null ? item["DeliveryAddress"].ToString() : "";
                    if (item["Flag"] != null)
                    {
                        cmodel.Flag = item["Flag"].ToString() != "0";
                    }
                    else
                    {
                        cmodel.Flag = false;
                    }

                    list.Add(cmodel);
                }
            }
            if (_dal.AddCustomer(model))
            {
                _dal.AddCustomerContacts(list);
            }
        }

        public string GetCustomerList(CustomerQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetCustomerList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetCustomerInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetCustomerInfo(id));
        }

        public string GetCustomerContactsList(CustomerContactsQcEntity qcmodel)
        {
            var list = _dal.GetCustomerContactsList(qcmodel);
            return JsonHelper.Encode(list);
        }

        public void ModifyCustomer(string data)
        {
            var model = new CustomerEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<CustomerContactsEntity>();
            foreach (Hashtable row in rows)
            {
                model.CustomerId = new Guid(row["CustomerId"].ToString());
                model.CustomerCode = row["CustomerCode"].ToString();
                model.CustomerName = row["CustomerName"].ToString();
                model.CCategoryId = new Guid(row["CCategoryId"].ToString());
                model.BalanceDate = Convert.ToDateTime(row["BalanceDate"]);
                model.InitialRec = Convert.ToDecimal(row["InitialRec"]);
                model.InitialDeposit = Convert.ToDecimal(row["InitialDeposit"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new CustomerContactsEntity();
                    cmodel.CContactId = Guid.NewGuid();
                    cmodel.CustomerId = model.CustomerId;
                    cmodel.Linkman = item["Linkman"] != null ? item["Linkman"].ToString() : "";
                    cmodel.MobilePhone = item["MobilePhone"] != null ? item["MobilePhone"].ToString() : "";
                    cmodel.SpecialPlane = item["SpecialPlane"] != null ? item["SpecialPlane"].ToString() : "";
                    cmodel.QqMsn = item["QqMsn"] != null ? item["QqMsn"].ToString() : "";
                    cmodel.DeliveryAddress = item["DeliveryAddress"] != null ? item["DeliveryAddress"].ToString() : "";
                    if (item["Flag"] != null)
                    {
                        cmodel.Flag = item["Flag"].ToString() != "0";
                    }
                    else
                    {
                        cmodel.Flag = false;
                    }

                    list.Add(cmodel);
                }
            }
            _dal.ModifyCustomer(model);
            _dal.DeleteCustomerContacts(model.CustomerId.ToString());
            _dal.AddCustomerContacts(list);
        }

        public bool RemoveCustomer(string id)
        {
            var result = false;
            if(_dal.RemoveCustomer(id))
            {
                _dal.DeleteCustomerContacts(id);
                result = true;
            }
            return result;
        }

        public string GetCustomerListByCombobox()
        {
            return JsonHelper.Encode(_dal.GetCustomerListByCombobox());
        }
    }
}
